package com.bos.prop.controller;

import com.bos.dao.KabupatenDAO;
import com.bos.dao.PeriodeDAO;
import com.bos.dao.SekolahDAO;
import com.bos.dao.StatusLaporanDAO;
import com.bos.domain.Kabupaten;
import com.bos.domain.Periode;
import com.bos.domain.Sekolah;
import com.bos.domain.StatusLaporan;
import com.bos.editor.KabupatenEditor;
import com.bos.editor.PeriodeEditor;
import com.bos.editor.SekolahEditor;
import com.bos.editor.StatusLaporanEditor;
import com.bos.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class StatusLaporanController {

    private StatusLaporanDAO statusLaporanDAO;
    private PeriodeDAO periodeDAO;
    private SekolahDAO sekolahDAO;
    private KabupatenDAO kabupatenDAO;
	private RestTemplate restTemplate;
    /*private Credentials credentials;*/


    /* --------------------- Getter and Setter ---------------------*/

    /*public Credentials getCredentials() {
        return credentials;
    }

    @Autowired
    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
        if (restTemplate != null) {
            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
            HttpClient client = factory.getHttpClient();
            client.getState().setCredentials(AuthScope.ANY, credentials);
        }
    }*/

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

//        if (credentials != null) {
//            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
//            HttpClient client = factory.getHttpClient();
//            client.getState().setCredentials(AuthScope.ANY, credentials);
//        }
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }


    @Autowired
    public void setStatusLaporanDAO(StatusLaporanDAO statusLaporanDAO) {
        this.statusLaporanDAO = statusLaporanDAO;
    }

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setSekolahDAO(SekolahDAO sekolahDAO) {
        this.sekolahDAO = sekolahDAO;
    }

    /* -------------------------- Other Methods -------------------------- */

    @RequestMapping(value = "/kabupaten/{kabupatenId}/sekolah/{npsn}/statusLaporan", method = RequestMethod.GET)
    public String index(ModelMap modelMap, @PathVariable("kabupatenId") Kabupaten kabupaten, @PathVariable("npsn") Sekolah sekolah) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("sekolah", sekolah);
        modelMap.addAttribute("kabupaten", kabupaten);

        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(kabupaten) + "sekolah/{npsn}/statusLaporan.json";
        Map<String, Object> varUrl = new HashMap<String, Object>();
        varUrl.put("npsn", sekolah.getNpsn());
        try {
            ResponseEntity<List> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.GET, null, List.class, varUrl);
            List<Sekolah> response = (List<Sekolah>) responseEntity.getBody();
            modelMap.addAttribute("data", response);
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }

        //atribute jika terjadi error saat melakukan pengambilan data dari host kabupaten
        if (!restError.isEmpty())
            modelMap.addAttribute("restError", restError.trim());

        return "statusLaporan/index";
    }

    @RequestMapping(value = "/kabupaten/{kabupatenId}/sekolah/{npsn}/statusLaporan/periode/{periodeId}", method = RequestMethod.GET)
    public String show(ModelMap modelMap, @PathVariable("kabupatenId") Kabupaten kabupaten, @PathVariable("npsn") Sekolah sekolah, @PathVariable("periodeId") Periode periode) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("sekolah", sekolah);
        modelMap.addAttribute("kabupaten", kabupaten);

        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(kabupaten) + "sekolah/{npsn}/statusLaporan/periode/{periodeId}.json";
        Map<String, Object> varUrl = new HashMap<String, Object>();
        varUrl.put("npsn", sekolah.getNpsn());
        varUrl.put("periodeId", periode.getId());
        try {
            ResponseEntity<StatusLaporan> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.GET, null, StatusLaporan.class, varUrl);
            StatusLaporan response = (StatusLaporan) responseEntity.getBody();
            modelMap.addAttribute("data", response);
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }

        //atribute jika terjadi error saat melakukan pengambilan data dari host kabupaten
        if (!restError.isEmpty())
            modelMap.addAttribute("restError", restError.trim());

        return "statusLaporan/show";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Sekolah.class, new SekolahEditor(sekolahDAO));
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(StatusLaporan.class, new StatusLaporanEditor(statusLaporanDAO));
    }
}
